package xyz.icxl.android.architecture.observer

import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer
import xyz.icxl.android.architecture.dialog.LoadingDialog
import xyz.icxl.android.architecture.ui.mvvm.BaseViewModel

/**
 * 页面进度状态观察者
 */
class PageLoadingStatusObserver(private val fragmentManager: FragmentManager) :
    Observer<BaseViewModel.PageLoadingStatus> {

    var loadingDialog: LoadingDialog? = null

    override fun onChanged(status: BaseViewModel.PageLoadingStatus?) {
        if (status?.show == true && (loadingDialog == null || loadingDialog?.isShowing == false)) {
            if (loadingDialog == null)
                loadingDialog = LoadingDialog(status.text ?: "···",status.isCancelable)
            loadingDialog?.show(fragmentManager)
        }
        if (status?.show == true && loadingDialog?.isShowing == true){
            loadingDialog?.changeText(status.text ?: "···")
        }
        if (status?.success == true && loadingDialog?.isShowing == true) {
            loadingDialog?.changeText(status.text ?: "成功")
            loadingDialog?.dismissBySuccess()
        }
        if (status?.fail == true && loadingDialog?.isShowing == true) {
            loadingDialog?.changeText(status.text ?: "失败")
            loadingDialog?.dismissByFail()
        }
        if (status?.dismiss == true && loadingDialog?.isShowing == true) {
            loadingDialog?.dismiss()
        }

    }


}